﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.Mvc;
using System.Web.Mvc.Html;
using System.Web;

namespace Microhard.Framework.MVCHelper
{
    public class BaseButton : IScriptableComponent
    {
        #region 属性

        protected string _gridID;

        protected string _id;

        protected string _assetKey;

        protected Dictionary<string, string> _passToActionValues;


        /// <summary>
        /// Grid控件ID
        /// </summary>
        public string GridID
        {
            get
            {
                return this._gridID;
            }
            set
            {
                this._gridID = value;
            }
        }

        /// <summary>
        /// 控件ID
        /// </summary>
        public string ID
        {
            get
            {
                return this._id;
            }
            set
            {
                this._id = value;
            }
        }

        /// <summary>
        ///控件唯一标识
        /// </summary>
        public string AssetKey
        {
            get
            {
                return this._assetKey;
            }
            set
            {
                this._assetKey = value;
            }
        }

        public Dictionary<string, string> PassToActionValues
        {
            get
            {
                return this._passToActionValues;
            }
            set
            {
                this._passToActionValues = value;
            }
        }

        protected string _areaName;

        public string AreaName
        {
            get { return this._areaName; }
            set { this._areaName = value; }
        }
        #endregion

        #region 方法
        protected MvcHtmlString CreateLinkButton(string operationKeyName, HtmlHelper helper, string id, string linkText, string actionName, string controllerName, object routeValue)
        {
            object ops = helper.ViewContext.HttpContext.Items[SysConst.OPERATION_ITEM_KEYNAME];
            if (!IsAuthorized(operationKeyName, ops))
                return MvcHtmlString.Empty;
            object ism = helper.ViewContext.HttpContext.Items[SysConst.ISMOBILERQUEST];
            if (ism != null && (bool)ism)
            {
                IDictionary<string, object> attributes = new Dictionary<string, object>();
                attributes.Add("id", id);
                attributes.Add("name", operationKeyName);
                attributes.Add("data-role", "button");
                attributes.Add("data-role", "button");
                attributes.Add("data-icon", "plus");
                attributes.Add("data-iconpos", "right");
                return LinkExtensions.ActionLink(helper, linkText, actionName, controllerName, routeValue, attributes);
            }
            return LinkExtensions.ActionLink(helper, linkText, actionName, controllerName, routeValue, new { @class = "t-button", id = id, name = operationKeyName });
        }

        protected MvcHtmlString CreateButton(string operationKeyName, HtmlHelper helper, string id, string text, bool isSubmit, string controller, string action, string formID)
        {
            object ops = helper.ViewContext.HttpContext.Items[SysConst.OPERATION_ITEM_KEYNAME];
            if (!IsAuthorized(operationKeyName, ops))
                return MvcHtmlString.Empty;
            var tag = new TagBuilder("input");
            if (isSubmit)
            {
                tag.Attributes["type"] = "submit";
            }
            else
            {
                tag.Attributes["type"] = "button";
            }
            tag.Attributes["class"] = "t-button";
            tag.Attributes["id"] = id;
            tag.Attributes["value"] = text;
            tag.Attributes["name"] = id;

            if (isSubmit)
            {
                var formTag = new TagBuilder("form");
                formTag.Attributes["method"] = "post";
                string url = HttpRuntime.AppDomainAppVirtualPath;
                formTag.Attributes["action"] = url + "/" + _areaName + "/" + controller + "/" + action;
                formTag.Attributes["id"] = formID;
                formTag.InnerHtml = tag.ToString();
                return new MvcHtmlString(formTag.ToString());
            }
            return new MvcHtmlString(tag.ToString());
        }


        protected static bool IsAuthorized(string operationKeyName, object hasOperations)
        {
            var ops = hasOperations as List<string>;
            if (ops != null)
            {
                return ops.Contains(operationKeyName);
            }
            return false;
        }


        public virtual string WriteJQueryReadyInnerScript(HttpContextBase helper)
        {
            return string.Empty;
        }

        public virtual string WriteJQueryReadyOutterScript(HttpContextBase helper)
        {
            return string.Empty;
        }

        public virtual MvcHtmlString WriteHtml(HtmlHelper helper, string linkText, string actionName, string controllerName)
        {
            return MvcHtmlString.Empty;
        }

        #endregion
    }
}
